package com.hexiang.service.impl;

import com.hexiang.domain.User;
import com.hexiang.mapper.UserMapper;
import com.hexiang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("userServiceImpl")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    @Cacheable(value = { "user" }, key = "#name", unless = "#result == null")
    public User findByName(String name) {
        return userMapper.findByName(name);
    }

    @Transactional
    @Override
    public User saveByUser(User user) {
        userMapper.insertByUser(user);
        return user;

    }

    @Transactional
    @Override
    public int save(String name, Integer age) {
        return userMapper.insert(name, age);
    }

    @Transactional
    @CacheEvict(value = "user", key = "#user.id")
    @Override
    public int update(User user) {
        return userMapper.update(user);
    }

    @Transactional
    @CacheEvict(value = "user", key = "#id")
    @Override
    public int delete(Long id) {
        return userMapper.delete(id);
    }

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }
}